package com.tanhua.system.controller;

import com.tanhua.system.enums.PublishAuditStateEnum;
import com.tanhua.system.service.MessageService;
import com.tanhua.system.vo.MessagePageResult;
import com.tanhua.system.vo.MessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("manage/messages")
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 消息翻页
     *
     * @return
     */
    @GetMapping
    public ResponseEntity<MessagePageResult> queryMessageList(
            @RequestParam(name = "page", defaultValue = "1") Integer page,     // 页数
            @RequestParam(name = "pagesize", defaultValue = "10") Integer pageSize, // 页尺寸
            @RequestParam(name = "sortProp", required = false) String sortProp,  // 排序字段
            @RequestParam(name = "sortOrder", required = false) String sortOrder, // ascending 升序 descending 降序
            @RequestParam(name = "id", required = false) String id, // 消息id
            @RequestParam(name = "sd", required = false) Long sd, //开始时间
            @RequestParam(name = "ed", required = false) Long ed, //结束时间
            @RequestParam(name = "uid", required = false) Long uid, //用户id
            @RequestParam(name = "state", required = false) String state) // 审核状态
    {
        try {
            // 消息翻页
            MessagePageResult messagePageResult = this.messageService.queryMessage(pageSize, page, sortProp, sortOrder, id, sd, ed, uid, state);
            if (messagePageResult != null) {
                return ResponseEntity.ok(messagePageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 消息置顶
     *
     * @param publishId 消息id
     * @return 操作结果
     */
    @PostMapping("/{id}/top")
    public ResponseEntity<String> topMsg(@PathVariable(name = "id") String publishId) {
        try {
            Boolean bool = this.messageService.topOrUnTop(publishId, 2);// 2 为置顶
            if (bool) {
                return ResponseEntity.ok("操作成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("操作失败");
    }

    /**
     * 取消消息置顶
     *
     * @param publishId 消息id
     * @return 操作结果
     */
    @PostMapping("/{id}/untop")
    public ResponseEntity<String> unTopMsg(@PathVariable(name = "id") String publishId) {
        try {
            Boolean bool = this.messageService.topOrUnTop(publishId, 1);// 1 为取消置顶
            if (bool) {
                return ResponseEntity.ok("操作成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("操作失败");
    }


    /**
     * 消息通过
     *
     * @param ids 消息id
     * @return 操作结果
     */
    @PostMapping("/pass")
    public ResponseEntity<String> paseMsg(@RequestBody List<String> ids) {
        try {
            Boolean bool = this.messageService.passOrReject(ids, Integer.parseInt(PublishAuditStateEnum.MAUL_PASS.getValue()));// 人工审核通过
            if (bool) {
                return ResponseEntity.ok("操作成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("操作失败");
    }

    /**
     * 消息拒绝
     *
     * @param ids 消息id
     * @return 操作结果
     */
    @PostMapping("/reject")
    public ResponseEntity<String> rejectMsg(@RequestBody List<String> ids) {
        try {
            Boolean bool = this.messageService.passOrReject(ids, Integer.parseInt(PublishAuditStateEnum.MAUL_BLOCK.getValue()));// 人工审核拒绝
            if (bool) {
                return ResponseEntity.ok("操作成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("操作失败");
    }

    /**
     * 消息撤销
     *
     * @param ids 消息id
     * @return 操作结果
     */
    @PostMapping("/revocation")
    public ResponseEntity<String> revocationMsg(@RequestBody List<String> ids) {
        try {
            Boolean bool = this.messageService.revocationMsg(ids);// 消息撤销
            if (bool) {
                return ResponseEntity.ok("操作成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("操作失败");
    }

    /**
     * 根据id查询消息详情
     *
     * @param publishId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<MessageVo> queryById(@PathVariable(name = "id") String publishId) {
        try {
            MessageVo messageVo = this.messageService.queryById(publishId);
            if (messageVo != null) {
                return ResponseEntity.ok(messageVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
